using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;
using FeedAntGame.Control;


namespace FeedAntGame.Component.Dialog
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PauseDlg : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D background;
        SpriteBatch spriteBatch;
        Button resumeButton;
        Vector2 startPos;

        public PauseDlg(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
          
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            background = Game.Content.Load<Texture2D>(@"Images\pause_background");
            startPos = new Vector2(Game.Window.ClientBounds.Width / 2 - background.Width / 2,
                Game.Window.ClientBounds.Height / 2 - background.Height / 2);
            resumeButton = new Button(Game, Button.TYPE.RESUME, startPos + new Vector2(100, 180));
            EventSprite resumeSprite = (EventSprite)resumeButton.Sprite;
            resumeSprite.Click += new EventSprite.ClickButton(onResumeClick);
            resumeSprite.Hover += new EventSprite.HoverButton(onShowHand);
            base.LoadContent();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            resumeButton.Sprite.Update(gameTime, Game.Window.ClientBounds);
            if ((resumeButton.Sprite as EventSprite).IsPlaySoundHover())
            {
                ((GameFA)Game).PlayCue(resumeButton.Sprite.collisionCueName);
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Rectangle((int)startPos.X, (int)startPos.Y, background.Width, background.Height), Color.White);
            resumeButton.Sprite.Draw(gameTime, spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void onResumeClick()
        {
            Finish();
        }
        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }

        public void Start()
        {
            this.Enabled = true;
            this.Visible = true;
            ((GameFA)Game).Volume(0, "Music");
        }

        private void Finish()
        {            
            this.Enabled = false;
            this.Visible = false;            
            ((GameFA)Game).Volume(1,"Music");
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is PauseDlg) && c.Visible == true && c.Enabled == false)
                {
                    c.Enabled = true;
                }
            }
        }
    }
}
